<html>
<input type="text" id="input"/>
<script type="text/javascript">

/* Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 */
// Inspired by base2 and Prototype
(function(){
  // initializing 开关很巧妙的来实现调用原型而不构造，还有回掉
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  // The base Class implementation (does nothing)
  // 全局，this 指向 window，最大的父类
  this.Class = function(){};
 
  // Create a new Class that inherits from this class
  // 继承的入口
  Class.extend = function(prop) {
    //保留当前类，一般是父类的原型
    var _super = this.prototype;
   
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    //开关 用来使原型赋值时不调用真正的构成流程
    initializing = true;
    var prototype = new this();
    initializing = false;
   
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      //对函数判断，将属性套到子类上
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          //用闭包来存储
          return function() {
            var tmp = this._super;
           
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
           
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            //实现同名调用
            var ret = fn.apply(this, arguments);  
            this._super = tmp;
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
   
    // 要返回的子类
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
    //前面介绍过的，继承
    Class.prototype = prototype;
   
    Class.prototype.constructor = Class;
 
    Class.extend = arguments.callee;
   
    return Class;
  };
})();

var Person = Class.extend({
  init: function(name){
    this.name = name;
  },
  Say: function(name){
    console.log(this.name + ' can Say!');
  },
  Sleep: function(){
    console.log(this.name + ' can Sleep!');
  }
});
var Student = Person.extend({
  init: function(name){
    this._super('Student-' + name);
  },
  Sleep: function(){
    this._super();
    console.log('And sleep early!');
  },
  DoHomeWork: function(){
    console.log(this.name + ' can do homework!');
  }
});
var p = new Person('Li');
p.Say(); //'Li can Say!'
p.Sleep(); //'Li can Sleep!'
var jerry = new Student('i042416');
jerry.Say(); //'Student-xiaojerry can Say!'
jerry.Sleep();//'Student-xiaojerry can Sleep!'
            // 'And sleep early!'
jerry.DoHomeWork(); //'Student-xiaojerry can do homework!'

var JudgeInput = Class.extend({
  init: function(config){
    this.input = document.getElementById(config.id);
    this._bind();
  },
  _getValue: function(){
    return this.input.value;
  },
  _render: function(){
    var value = this._getValue();
    if(!document.getElementById("show")){
      var append = document.createElement('span');
      append.setAttribute("id", "show");
      input.parentNode.appendChild(append);
    }
    var show = document.getElementById("show");
    if(/^[0-9a-zA-Z]+$/.exec(value)){
      show.innerHTML = 'Pass!';
    }else{
      show.innerHTML = 'Failed!';
    }
  },
  _bind: function(){
    var self = this;
    self.input.addEventListener('keyup', function(){
      self._render();
    });
  }
});
window.onload = function(){
  new JudgeInput({id: "input"});
}

</script>
</html>